<?php

namespace app\admin\model\agent;


use app\common\exception\BaseException;
use think\Model;
use think\Session;
use think\Db;

/**
 * 代理商用户模型
 * Class StoreUser
 * @package app\admin\model
 */
class User extends Model
{

    protected $name = 'agent_user';


    /**
     * 关联用户角色表表
     * @return \think\model\relation\BelongsToMany
     */
    public function role()
    {
        return $this->belongsToMany('Role', 'AgentUserRole');
    }






    /**
     * 验证用户名是否重复
     * @param $userName
     * @return bool
     */
    public static function checkExist($userName)
    {
        return self::where('user_name', '=', $userName)->where('is_delete', '=', 0)->value('agent_user_id');
    }

    /**
     * 商家用户详情
     * @param $where
     * @param array $with
     * @return \app\agent\model\agent\User|null
     * @throws \think\exception\DbException
     */
    public static function detail($where, $with = [])
    {
        !is_array($where) && $where = ['agent_user_id' => (int)$where];
        return static::get(array_merge(['is_delete' => 0], $where), $with);
    }



    /**
     * 关联微信代理商表
     * @return \think\model\relation\BelongsTo
     */
    public function agent()
    {
        return $this->belongsTo("Agent",'agent_id','agent_id');
    }
    /**
     * 新增商家用户记录
     * @param int $agent_id
     * @param array $data
     * @return bool|false|int
     */
    public function add($agent_id, $data)
    {

        return $this->save([
            'user_name'    => $data['user_name'],
            'password'     => ($data['password']),
            'salt'         => ($data['salt']),
            'is_super'     => 1,
            'type'         => 0,
            'agent_id'     => $agent_id,
            'company_name' => $data['company_name'],
            'real_name'    => $data['real_name'],
            'company_tel'  => $data['company_tel'],
            'pinpai_id'    => $agent_id,
            'employees_num'=>$data['employees_num'],
            'status'       => 0,
            'active'       => 0,
        ]);
    }

    /**
     * 商家用户登录
     * @param int $agentId
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login($agentId)
    {
        // 获取获取商城超级管理员用户信息
        $user = $this->getSuperStoreUser($agentId);
        if (empty($user)) {
            throw new BaseException(['msg' => '超级管理员用户信息不存在']);
        }
        $this->loginStates($user);
    }

    /**
     * 获取获取商城超级管理员用户信息
     * @param $agentId
     * @return User|null
     * @throws \think\exception\DbException
     */
    private function getSuperStoreUser($agentId)
    {
        return static::info(['agent_id' => $agentId, 'is_super' => 1], ['agent']);
    }

    /**
     * 商家用户详情
     * @param $where
     * @param array $with
     * @return static|null
     * @throws \think\exception\DbException
     */
    public static function info($where, $with = [])
    {
        !is_array($where) && $where = ['agent_user_id' => (int)$where];
        $where['is_delete']=0;
        $user=Db::name('agent_user')->where($where)->find();
        $user['agent']=Db::name('agent')->where('agent_id',$user['agent_id'])->find();
        return  $user;
       // return static::get(array_merge(['is_delete' => 0], $where), $with);
    }
    /**
     * 删除小程序下的商家用户
     * @param $agentId
     * @return false|int
     */
    public function setDeletes($agentId)
    {
        return $this->save(['is_delete' => 1], ['agent_id' =>$agentId]);
    }
    /**
     * 保存登录状态
     * @param $user
     * @throws \think\Exception
     */
    public function loginStates($user)
    {
        $agent = $user['agent'];
        // 保存登录状态
        Session::set('yoshop_agent', [
            'user' => [
                'agent_user_id' => $user['agent_user_id'],
                'user_name' => $user['user_name'],
            ],
            'agent' => $agent,
            'is_login' => true,
        ]);
    }


}
